package P6.T03;

import javax.swing.*;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

public class JMyPanel extends JPanel {
    private List<MyLine> lines=new ArrayList<>();
    private MyLine l=null;
    public JMyPanel() {
        MyMouseListener mml=new MyMouseListener(this);
        addMouseListener(mml);
        addMouseMotionListener(mml);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.clearRect(0,0,getWidth(),getHeight());
        for(MyLine ll:lines)ll.draw(g);
        if(l!=null)l.draw(g);
    }

    public void StartDraw(int x, int y) {
        l=new MyLine();
        l.setStart(x,y);
        l.setEnd(x,y);
        repaint();
    }

    public void drawing(int x, int y) {
        l.setEnd(x,y);
        repaint();
    }

    public void drawOver() {
        lines.add(l);
        l=null;
    }
}
